/**
 * Copyright (C) 2022-2023
 * All rights reserved, Designed By www.jjsjshop.com
 * 注意：
 * 本软件为www.jjsjshop.com开发研制，项目使用请保留此说明
 */
const app = getApp()

Page({
    data: {
        orderConfirmData: [],
        salesPrice: 0,
        paymentPrice: 0,
        freightPrice: 0,
        userAddress: null,
        userReceipt: null,
        orderSubParm: {
            paymentType: '1',
            deliveryWay: '1'
        },
        loading: false,
        userInfo: null,
        spuIds: [],
        mallSetting: {},
        isSelf: false,
        address: {
            name: null,
            phone: null
        }
    },
    onShow() {

    },
    onLoad: function () {
        this.userAddressPage()
        this.userReceiptPage()
        this.userInfoGet()
        this.orderConfirmDo()
        this.getMallSetting()
    },
    deliveryWayChange(e) {
        this.setData({
            [`orderSubParm.deliveryWay`]: e.detail.value,
            freightMap: null
        })
        this.orderConfirmDo()
    },
    orderConfirmDo() {
        // 本地获取参数信息
        let that = this
        wx.getStorage({
            key: 'param-orderConfirm',
            success: function (res) {
                let orderConfirmData = res.data
                let salesPrice = 0 //订单金额
                let freightPrice = 0 //运费
                let spuIds = null
                orderConfirmData.forEach((orderConfirm, index) => {
                    if (spuIds) {
                        spuIds = spuIds + ',' + orderConfirm.spuId
                    } else {
                        spuIds = orderConfirm.spuId
                    }
                    salesPrice = (Number(salesPrice) + orderConfirm.salesPrice * orderConfirm.quantity).toFixed(2)
                    orderConfirm.paymentPrice = (orderConfirm.salesPrice * orderConfirm.quantity).toFixed(2)
                    //计算运费
                    orderConfirm.freightPrice = 0
                    freightPrice = (Number(freightPrice) + Number(orderConfirm.freightPrice)).toFixed(2)
                })
                that.setData({
                    orderConfirmData: orderConfirmData,
                    salesPrice: salesPrice,
                    freightPrice: freightPrice,
                    paymentPrice: salesPrice,
                    spuIds: spuIds
                })

            }
        })
    },
    //计算运费
    countFreight(orderConfirm, freightTemplat, quantity) {

    },
    //获取默认收货地址
    userAddressPage() {
        app.api.userAddressPage({
            searchCount: false,
            current: 1,
            size: 1,
            isDefault: '1'
        })
            .then(res => {
                let records = res.data.records
                if (records && records.length > 0) {
                    this.setData({
                        userAddress: records[0]
                    })
                }
            })
    },
    //获取默认发票信息
    userReceiptPage() {
        app.api.userReceiptPage({
            searchCount: false,
            current: 1,
            size: 1,
            isDefault: '1'
        })
            .then(res => {
                let records = res.data.records
                if (records && records.length > 0) {
                    this.setData({
                        userReceipt: records[0]
                    })
                }
            })
    },
    //获取商城用户信息
    userInfoGet() {
        app.api.wxUserGet()
            .then(res => {
                this.setData({
                    userInfo: res.data
                })
            })
    },
    userMessageInput(e) {
        this.setData({
            [`orderSubParm.userMessage`]: e.detail.value
        })
    },
    //提交订单
    orderSub() {
        let that = this
        let userAddress = that.data.userAddress
        if (this.data.isSelf == true) {
            if (!(this.data.address.name && this.data.address.phone)) {
                wx.showToast({
                    title: '请将自提人信息填写完整',
                    icon: 'none',
                    duration: 2000
                })
                return
            } else if (!/^1[3456789]\d{9}$/.test(this.data.address.phone)) {
                wx.showToast({
                    title: '联系人电话格式错误',
                    icon: 'none',
                });
                return;
            }
        } else {
            if (that.data.orderSubParm.deliveryWay == '1' && userAddress == null) {
                wx.showToast({
                    title: '请选择收货地址',
                    icon: 'none',
                    duration: 2000
                })
                return
            }
        }
        let userReceipt = that.data.userReceipt
        if (userReceipt == null) {
            wx.showModal({
                title: '提示',
                content: '确认不需要发票吗？',
                success(res) {
                    if (res.confirm) {
                        that.submitToServer(userAddress, userReceipt, that);
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                        return;
                    }
                }
            })
        } else {
            this.submitToServer(userAddress, userReceipt, that);
        }
    },
    submitToServer(userAddress, userReceipt, that) {
        that.setData({
            loading: true
        })
        let orderSubParm = that.data.orderSubParm
        orderSubParm.skus = that.data.orderConfirmData
        orderSubParm.isSelf = that.data.isSelf == true ? '1' : '0'
        orderSubParm.userAddressName = that.data.address.name
        orderSubParm.userAddressPhone = that.data.address.phone
        orderSubParm.userAddressDetail = that.data.mallSetting.selfInfo
        app.api.orderSub(Object.assign({}, {
                userAddressId: that.data.isSelf == true ? null : userAddress.id,
                userReceiptId: userReceipt == null ? null : userReceipt.id
            },
            orderSubParm
        ))
            .then(res => {
                wx.redirectTo({
                    url: '/pages/order/order-detail/index?callPay=true&id=' + res.data.id
                })
            }).catch(() => {
            that.setData({
                loading: false
            })
        })
    },
    getMallSetting() {
        app.api.mallSetting(null).then(res => {
            if (res && res.data && res.data != '') {
                this.setData({
                    mallSetting: res.data
                })
            } else {
                this.setData({
                    mallSetting: {
                        expressInfo: '',
                        selfInfo: ''
                    }
                })
            }
        })
    },
    /*
     *是否自提
     *
     */
    switchChange: function (e) {
        console.log(e.detail.value);
        this.setData({
            isSelf: e.detail.value
        });
    },
    changeSelfName(e) {
        console.log(e.detail.value)
        let address = this.data.address
        address.name = e.detail.value;
        if (e.detail.value.length == 0) {
            address.name = null
        }
        this.setData({
            address: address
        });
    },

    changeSelfPhone(e) {
        console.log(e.detail.value);
        let address = this.data.address
        address.phone = e.detail.value;
        if (e.detail.value.length == 0) {
            address.phone = null
        }
        this.setData({
            address: address
        });
    },
    //数量变化
    stNumMinus(e) {
        let index = e.target.dataset.index
        let orderConfirm = this.data.orderConfirmData[index]
        if (orderConfirm != null && orderConfirm != undefined) {
            orderConfirm.quantity = orderConfirm.quantity - 1
            let data = this.data.orderConfirmData;
            data[index] = orderConfirm
            this.setData({
                orderConfirmData: data
            })
            this.quantityChange();
        }
    },
    stNumAdd(e) {
        let index = e.target.dataset.index
        let orderConfirm = this.data.orderConfirmData[index]
        if (orderConfirm != null && orderConfirm != undefined) {
            orderConfirm.quantity = orderConfirm.quantity + 1
            let data = this.data.orderConfirmData;
            data[index] = orderConfirm
            this.setData({
                orderConfirmData: data
            })
            this.quantityChange();
        }
    },
    numChange(e) {
        let quantity = e.detail.value
        let index = e.target.dataset.index
        let orderConfirm = this.data.orderConfirmData[index]
        if (orderConfirm != null && orderConfirm != undefined) {
            orderConfirm.quantity = quantity
            let data = this.data.orderConfirmData;
            data[index] = orderConfirm
            this.setData({
                orderConfirmData: data
            })
            this.quantityChange();
        }
    },
    quantityChange() {
        let salesPrice = 0 //订单金额
        let freightPrice = 0 //运费
        this.data.orderConfirmData.forEach((orderConfirm, index) => {
            salesPrice = (Number(salesPrice) + orderConfirm.salesPrice * orderConfirm.quantity).toFixed(2)
            orderConfirm.paymentPrice = (orderConfirm.salesPrice * orderConfirm.quantity).toFixed(2)
            //计算运费
            orderConfirm.freightPrice = 0
            freightPrice = (Number(freightPrice) + Number(orderConfirm.freightPrice)).toFixed(2)
        })
        this.setData({
            salesPrice: salesPrice,
            freightPrice: freightPrice,
            paymentPrice: salesPrice,
        })
    }

})
